package com.guigu.controller;

import com.guigu.pojo.Stock;
import com.guigu.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/stock")
public class StockController {

    @Autowired
    private StockService stockService;

    @GetMapping("/list")
    public Map<String, Object> getStockList(@RequestParam(required = false) String name,
                                            @RequestParam(required = false) String type,
                                            @RequestParam(defaultValue = "1") Integer page,
                                            @RequestParam(defaultValue = "10") Integer size) {
        return stockService.getStockList(name, type, page, size);
    }

    @PutMapping("/update")
    public Map<String, Object> updateStock(@RequestBody Stock stock) {
        Map<String, Object> result = new HashMap<>();
        boolean success = stockService.updateStock(stock);
        result.put("success", success);
        if (!success) {
            result.put("message", "修改仓库信息失败");
        }
        return result;
    }

    @GetMapping
    @ResponseBody
    public List<Stock> getAllStock() {
        List<Stock> list = stockService.list();
        System.out.println(list);

        return list;
    }
}
